//
//  KLGoodCell.m
//  KLShoppingList
//
//  Created by apple on 2017/8/17.
//  Copyright © 2017年 NetEaseExam. All rights reserved.
//

#import "KLGoodCell.h"

#import "KLGoodModel.h"

#import "KLConfig.h"

@interface KLGoodCell ()
@property (weak, nonatomic) IBOutlet UIButton *selectedBtn;
@property (weak, nonatomic) IBOutlet UIImageView *goodImgView;
@property (weak, nonatomic) IBOutlet UILabel *goodPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodAmountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *openTaxImgView;
@property (weak, nonatomic) IBOutlet UILabel *goodTaxLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodTitleLabel;
@property (weak, nonatomic) IBOutlet UIButton *goodTaxBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *goodTaxDetailHeight;
@property (weak, nonatomic) IBOutlet UIView *taxDetailView;
@property (weak, nonatomic) IBOutlet UIView *goodMainView;
@property (weak, nonatomic) IBOutlet UILabel *goodTaxDetailLabel;

@end

@implementation KLGoodCell

+(instancetype)cellWithTableView:(UITableView *)tableView{

    KLGoodCell *cell = [tableView dequeueReusableCellWithIdentifier:good_reuse_id];
//    if (cell==nil) {
//        cell = [[[NSBundle mainBundle] loadNibNamed:@"KLGoodCell" owner:nil options:nil] firstObject];
//    }
    return cell;
}
- (IBAction)goodSelectedBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(goodSelected:)]) {
        [self.delegate goodSelected:self];
    }
}
- (IBAction)taxDetailBtnClicked:(UIButton *)sender {
    
    sender.selected = !sender.selected;

    if (self.delegate && [self.delegate respondsToSelector:@selector(goodTaxDetail:)]) {
        [self.delegate goodTaxDetail:self];
    }
}


- (void)setGoodModel:(KLGoodModel *)goodModel{
    _goodModel = goodModel;
    self.goodTitleLabel.text = goodModel.title;
    self.goodImgView.image = [UIImage imageNamed:goodModel.image];
    
    self.goodPriceLabel.text = [NSString stringWithFormat:@"￥%@",goodModel.price];
    ;
    self.goodTaxLabel.text = [NSString stringWithFormat:@"税费:￥%@",goodModel.tax];
    if (goodModel.tax.floatValue>0) {
        self.goodTaxDetailLabel.text = [NSString stringWithFormat:@"预估税费:￥%@，小考拉已经努力帮您减免过部分税费哦",goodModel.tax];
    }else{
        self.goodTaxDetailLabel.text = [NSString stringWithFormat:@"预估税费:￥%@，商品已经包税，无需再次缴纳",goodModel.tax];
    }
    
    self.goodAmountLabel.text = [NSString stringWithFormat:@"x%@",goodModel.amount];
    
    self.selectedBtn.selected = goodModel.goodIsChoosed;
    
    if (goodModel.openTaxDetail) {
        self.taxDetailView.hidden = NO;
        self.goodTaxDetailHeight.constant = 30.0;
        self.openTaxImgView.transform = CGAffineTransformRotate(self.openTaxImgView.transform, M_PI);
//        self.openTaxImgView.image = [UIImage imageNamed:@"up"];
    }else{
        self.taxDetailView.hidden = YES;
        self.goodTaxDetailHeight.constant = 0.0;
        self.openTaxImgView.transform = CGAffineTransformIdentity;
//        self.openTaxImgView.image = [UIImage imageNamed:@"down"];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
